<style>
.disable {
    background-color: #ededed;
}
.onekeyCode .table > tbody > tr > td{
	line-height:1.7;
}
.oneKeysearchBar{
    position: absolute;
    top: 10px;
	right:15px;
    z-index: 20;
}
.success-msg .suc-con {
    float: right;
    width: 280px;
}
.bt-progress {
    background-color: #e2e2e2;
    border-radius: 8px;
    height: 16px;
    line-height: 16px;
    position: relative;
	margin:5px 0;
}
.bt-progress-bar {
    background-color: #5ab76c;
    border-radius: 8px;
    height: 16px;
    max-width: 100%;
    position: absolute;
    text-align: right;
    transition: all 0.3s ease 0s;
    width: 0;
}
.bt-progress-text {
	font-size: 12px;
    color: #fff;
    padding: 0 10px;
    position: static;
}
.onekey-menu-sub{
	background-color: #fff;
    border-bottom: 1px solid #ccc;
    height: 50px;
    line-height: 50px;
}
.onekey-menu-sub span{
	height: 50px;
    padding: 0 15px;
	cursor: pointer;
    display: inline-block;
    font-size: 14px;
}
.onekey-menu-sub .on {
    border-bottom: 2px solid #20a53a;
    color: #20a53a;
    font-weight: bold;
}
</style>
<div class="oneKeysearchBar">
	<input class="searchInput bt-input-text" type="text" placeholder="请输入检索条件" value="" style="width:140px" />
	<button class="btn btn-default btn-sm va0" onclick="GetSrcList();">查找</button>
</div>
<div class="onekey-menu-sub mlr15 onekey-type"></div>
<div class="divtable pd15 onekeyCode" style="max-height:500px; overflow:auto">
	<table class="table table-hover" width="100%">
		<thead>
			<tr>
				<th>名称</th>
				<th>版本</th>
				<th>简介</th>
				<th>支持PHP版本</th>
				<th style="text-align: right;" width="80">操作</th>
			</tr>
		</thead>
		<tbody id="zipBody"></tbody>
	</table>
</div>
<script type="javascript/text">
	var openId  = add = null;
	function AddDeployment(maction){
		if(maction == 1){
			var pdata = 'title=' + $("input[name='title']").val()
						+'&dname=' + $("input[name='name']").val()
						+'&ps=' + $("input[name='ps']").val()
						+'&version=' + $("input[name='version']").val()
						+'&rewrite=' + ($("input[name='rewrite']").attr('checked')?1:0)
						+'&shell=' + ($("input[name='shell']").attr('checked')?1:0)
						+'&php=' + $("input[name='php']").val()
						+'&md5=' + $("input[name='md5']").val()
						+'&download=' + $("input[name='download']").val()
			var loadT = layer.msg('正在提交 <img src="/static/img/ing.gif">',{icon:16,time:0,shade: [0.3, '#000']});
			$.post('/plugin?action=a&name=deployment&s=AddPackage',pdata,function(rdata){
				layer.close(loadT);
				layer.msg(rdata.msg,{icon:rdata.status?1:5});
				if(rdata.status) {
					GetSrcList();
					layer.close(openId);
				}
			});
			
			return;
		}
		openId = layer.open({
			type: 1,
			skin: 'demo-class',
			area: '480px',
			title: '添加源码包',
			closeBtn: 2,
			shift: 5,
			shadeClose: false,
			content:'标题：<input type="text" name="title"><br>\
					标识：<input type="text" name="name"><br>\
					描述：<input type="text" name="ps"><br>\
					版本：<input type="text" name="version"><br>\
					是否写伪静态：<input type="checkbox" name="rewrite"><br>\
					是否执行安装脚本：<input type="checkbox" name="shell"><br>\
					支持的PHP版本：<input type="text" name="php"><br>\
					md5：<input type="text" name="md5">\
					下载地址：<input type="text" name="download"><br>\
					<button class="btn btn-default btn-sm" onclick="AddDeployment(1);">提交</button>'
		});
	}
	
	
	$(".searchInput").keyup(function(e){
		if(e.keyCode == 13){
			GetSrcList();
		}
	});
	
	function AddSite(codename){
		var array;
		var str="";
		var domainlist='';
		var domain = array = $("#mainDomain").val().split("\n");
		var Webport=[];
		var checkDomain = domain[0].split('.');
		if(checkDomain.length < 1){
			layer.msg('域名格式不正确，请重新输入!',{icon:2});
			return;
		}
		for(var i=1; i<domain.length; i++){
			domainlist += '"'+domain[i]+'",';
		}
		Webport = domain[0].split(":")[1];//主域名端口
		if(Webport==undefined){
			Webport="80";
		}
		domainlist = domainlist.substring(0,domainlist.length-1);//子域名json
		mainDomain = domain[0].split(':')[0];
		domain ='{"domain":"'+domain[0]+'","domainlist":['+domainlist+'],"count":'+domain.length+'}';//拼接joson
		var php_version = $("select[name='version']").val();
		var loadT = layer.msg('正在创建站点 <img src="/static/img/ing.gif">',{icon:16,time:0,shade: [0.3, "#000"]})
		var data = $("#addweb").serialize()+"&port="+Webport+"&webname="+domain+'&ftp=false&sql=true&address=localhost&codeing=utf8&version='+php_version;
		$.post('/site?action=AddSite', data, function(ret) {
			layer.close(loadT)
			if(!ret.siteStatus){
				layer.msg(ret.msg,{icon:5});
				return;
			}
			layer.close(add)
			var sqlData = '';
			if (ret.databaseStatus) {
				sqlData = "<p class='p1'>数据库账号资料</p>\
					 		<p><span>数据库名：</span><strong>" + ret.databaseUser + "</strong></p>\
					 		<p><span>用户：</span><strong>" + ret.databaseUser + "</strong></p>\
					 		<p><span>密码：</span><strong>" + ret.databasePass + "</strong></p>\
					 		"
			}
			var pdata = 'dname=' + codename + '&site_name=' + mainDomain + '&php_version='+php_version;
			var loadT = layer.msg('<div class="depSpeed">正在提交 <img src="/static/img/ing.gif"></div>',{icon:16,time:0,shade: [0.3, "#000"]});
			
			setTimeout(function(){
				GetSpeed();
			},2000);
			
			$.post('/plugin?action=a&name=deployment&s=SetupPackage',pdata,function(rdata){
				layer.close(loadT)
				if(!rdata.status){
					layer.msg('源码下载失败!',{icon:5});
					return;
				}
				
				if(rdata.msg.username != ''){
					sqlData = "<p class='p1'>已成功部署，无需安装，请登录修改默认账号密码</p>\
					 		<p><span>用户：</span><strong>" + rdata.msg.username + "</strong></p>\
					 		<p><span>密码：</span><strong>" + rdata.msg.password + "</strong></p>\
					 		"
				}
				sqlData += "<p><span>访问站点：</span><a class='btlink' href='http://" + mainDomain + rdata.msg.install +  "' target='_blank'>http://" + mainDomain + rdata.msg.install + "</a></p>";

				layer.open({
					type: 1,
					area: '600px',
					title: '已成功部署'+codename,
					closeBtn:2,
					shadeClose: false,
					content: "<div class='success-msg'>\
						<div class='pic'><img src='/static/img/success-pic.png'></div>\
						<div class='suc-con'>\
							" + sqlData + "\
						</div>\
					 </div>",
				});
				if ($(".success-msg").height() < 150) {
					$(".success-msg").find("img").css({
						"width": "150px",
						"margin-top": "30px"
					});
				}
			});
			
			
		});
		
	}
	
	function GetSpeed(){
		if(!$('.depSpeed')) return;
		$.get('/plugin?action=a&name=deployment&s=GetSpeed',function(speed){
			if(speed.status === false) return;
			if(speed.name == '下载文件'){
				speed = '<p>正在'+speed.name+' <img src="/static/img/ing.gif"></p>\
				<div class="bt-progress"><div class="bt-progress-bar" style="width:'+speed.pre+'%"><span class="bt-progress-text">'+speed.pre+'%</span></div></div>\
				<p class="f12 c9"><span class="pull-left">'+ToSize(speed.used)+'/'+ToSize(speed.total)+'</span><span class="pull-right">'+ToSize(speed.speed)+'/s</span></p>';
				$('.depSpeed').prev().hide();
				$('.depSpeed').css({"margin-left":"-37px","width":"380px"});
				$('.depSpeed').parents(".layui-layer").css({"margin-left":"-100px"});
			}else{
				speed = '<p>'+speed.name+'</p>';
				$('.depSpeed').prev().show();
				$('.depSpeed').removeAttr("style");
				$('.depSpeed').parents(".layui-layer").css({"margin-left":"0"});
			}
			
			$('.depSpeed').html(speed);
			setTimeout(function(){
				GetSpeed();
			},1000);
		});
	}
	
	function onekeyCodeSite(codename,versions){
		$.post('/site?action=GetPHPVersion',function(rdata){
			var php_version = "";
			var n = 0;
			for(var i=rdata.length-1;i>=0;i--){
				if(versions.indexOf(rdata[i].version) != -1){
					php_version += "<option value='"+rdata[i].version+"'>"+rdata[i].name+"</option>";
					n++;
				}
	       }
			
			if(n == 0){
				layer.msg('缺少被支持的PHP版本，请安装!',{icon:5});
				return;
			}
			var con='<form class="bt-form pd20 pb70" id="addweb">\
					<div class="line"><span class="tname">域名</span>\
						<div class="info-r c4"><textarea id="mainDomain" class="bt-input-text" name="webname" style="width:398px;height:100px;line-height:22px"></textarea>\
							<div class="placeholder c9" style="top:10px;left:10px">每行填写一个域名，默认为80端口<br>泛解析添加方法 *.domain.com<br>如另加端口格式为 www.domain.com:88</div>\
						</div>\
					</div>\
					<div class="line"><span class="tname">备注</span>\
						<div class="info-r c4"><input id="Wbeizhu" class="bt-input-text" name="ps" placeholder="网站备注" style="width:398px" type="text"> </div>\
					</div>\
					<div class="line"><span class="tname">根目录</span>\
						<div class="info-r c4"><input id="inputPath" class="bt-input-text mr5" name="path" value="/www/wwwroot/" placeholder="网站根目录" style="width:398px" type="text"><span class="glyphicon glyphicon-folder-open cursor" onclick="ChangePath(\'inputPath\')"></span> </div>\
					</div>\
					<div class="line"><span class="tname">数据库</span>\
						<div class="info-r c4">\
							<input id="datauser" class="bt-input-text" name="datauser" placeholder="用户名/数据库名" style="width:190px;margin-right:13px" type="text">\
							<input id="datapassword" class="bt-input-text" name="datapassword" placeholder="密码" style="width:190px" type="text">\
						</div>\
					</div>\
					<div class="line"><span class="tname">源码</span>\
						<input class="bt-input-text mr5 disable" name="code" style="width:100px" value="'+codename+'" disabled>\
						<span class="c9">准备为你部署的源码程序</span>\
					</div>\
					<div class="line"><span class="tname">PHP版本</span>\
						<select class="bt-input-text mr5" name="version" id="c_k3" style="width:100px">\
							'+php_version+'\
						</select>\
						<span class="c9">请选择源码程序支持的php版本</span>\
					</div>\
					<div class="bt-form-submit-btn">\
						<button type="button" class="btn btn-danger btn-sm onekeycodeclose">取消</button>\
						<button type="button" class="btn btn-success btn-sm" onclick="AddSite(\''+codename+'\')">提交</button>\
					</div>\
				</from>';
			add = layer.open({
				type: 1,
				title: "宝塔一键部署"+codename,
				area: '550px',
				closeBtn: 2,
				shadeClose: false,
				content:con
			});
			var placeholder = "<div class='placeholder c9' style='top:10px;left:10px'>每行填写一个域名，默认为80端口<br>泛解析添加方法 *.domain.com<br>如另加端口格式为 www.domain.com:88</div>";
			$(".onekeycodeclose").click(function(){
				layer.close(add);
			});
			$('#mainDomain').after(placeholder);
			$(".placeholder").click(function(){
				$(this).hide();
				$('#mainDomain').focus();
			})
			$('#mainDomain').focus(function() {
				$(".placeholder").hide();
			});
			
			$('#mainDomain').blur(function() {
				if($(this).val().length==0){
					$(".placeholder").show();
				}  
			});
			//FTP账号数据绑定域名
			$('#mainDomain').on('input', function() {
				var defaultPath = '/www/wwwroot';
				var array;
				var res,ress;
				var str = $(this).val();
				var len = str.replace(/[^\x00-\xff]/g, "**").length;
				array = str.split("\n");
				ress =array[0].split(":")[0];
				res = ress.replace(new RegExp(/([-.])/g), '_');
				if(res.length > 15) res = res.substr(0,15);
				if($("#inputPath").val().substr(0,defaultPath.length) == defaultPath) $("#inputPath").val(defaultPath+'/'+ress);
				if(!isNaN(res.substr(0,1))) res = "sql"+res;
				if(res.length > 15) res = res.substr(0,15);
				$("#Wbeizhu").val(ress);
				$("#datauser").val(res);
			})
			$('#Wbeizhu').on('input', function() {
				var str = $(this).val();
				var len = str.replace(/[^\x00-\xff]/g, "**").length;
				if (len > 20) {
					str = str.substring(0, 20);
					$(this).val(str);
					layer.msg('不要超出20个字符', {
						icon: 0
					});
				}
			})
			//获取当前时间时间戳，截取后6位
			var timestamp = new Date().getTime().toString();
			var dtpw = timestamp.substring(7);
			$("#datauser").val("sql" + dtpw);
			$("#datapassword").val(_getRandomString(10));
		});
	}

	//生成n位随机密码
	function _getRandomString(len) {
		len = len || 32;
		var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; // 默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1  
		var maxPos = $chars.length;
		var pwd = '';
		for (i = 0; i < len; i++) {
			pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
		}
		return pwd;
	}
	function GetSrcList(){
		var loadT = layer.msg('正在获取列表 <img src="/static/img/ing.gif">',{icon:16,time:0,shade: [0.3, '#000']});
		var search = $(".searchInput").val();
		if(search != ''){
			search = '&search=' + search+'&';
		}
		var type = '';
		var istype = getCookie('depType');
		if(istype == 'undefined' || istype == 'null' || !istype) {
			istype = '0';
		}
		type = '&type='+istype;
		
		$.get('/plugin?action=a&name=deployment&s=GetList' + search + type,function(mlist){
			layer.close(loadT);
			
			tbody = '';
			for(var i=0;i<mlist.type.length;i++){
				var c = '';
				if(istype == mlist.type[i].type){
					c = 'class="on"';
				}
				tbody += '<span typeid="'+mlist.type[i].type+'" '+c+'>'+mlist.type[i].title+'</span>';
			}
			
			$(".onekey-type").html(tbody);
			
			var zbody = '';
			for(var i=mlist.data.length-1;i>=0;i--){
				zbody += '<tr>'
							+'<td>'+mlist.data[i].title+'</td>'
							+'<td>'+mlist.data[i].version+'</td>'
							+'<td>'+mlist.data[i].ps+'</td>'
							+'<td>'+mlist.data[i].php+'</td>'
							+'<td class="text-right"><a href="javascript:onekeyCodeSite(\''+mlist.data[i].name+'\',\''+mlist.data[i].php+'\');" class="btlink">一键部署</a></td>'
						+'</tr>'
			}
			$("#zipBody").html(zbody);
			$(".searchInput").val('');
			
			$(".onekey-menu-sub span").click(function(){
				setCookie('depType',$(this).attr('typeid'));
				$(this).addClass("on").siblings().removeClass("on");
				GetSrcList();
			});
		});
	}
	GetSrcList();
</script>